package com.fido.screen_adapter.utils

import com.fido.screen_adapter.constants.DimenTypes
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @author: FiDo
 * @date: 2024/11/11
 * @des:
 */
object MakeUtils {

    private const val XML_HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
    private const val XML_RESOURCE_START = "<resources>\r\n"
    private const val XML_RESOURCE_END = "</resources>\r\n"
    private const val XML_DIMEN_TEMPLETE = "<dimen name=\"%1\$spx_%2\$d\">%3$.2fdp</dimen>\r\n"

    private const val XML_BASE_DPI = "<dimen name=\"base_dpi\">%ddp</dimen>\r\n"

    var MAX_SIZE: Int = 720

    private const val XML_NAME = "dimens.xml"

    //Java 9 及以上版本中，BigDecimal.ROUND_HALF_UP 被标记为弃用，推荐改用 RoundingMode 枚举中的 RoundingMode.HALF_UP 来替代原有的四舍五入方式
//    fun px2dip(pxValue: Float, sw: Int, designWidth: Int): Float {
//        val dpValue = (pxValue / designWidth.toFloat()) * sw
//        val bigDecimal = BigDecimal(dpValue.toDouble())
//        val finDp = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).toFloat()
//        return finDp
//    }

    private fun px2dip(pxValue: Float, sw: Int, designWidth: Int): Float {
        val dpValue = (pxValue / designWidth.toFloat()) * sw
        val bigDecimal = BigDecimal(dpValue.toDouble())
        val finDp = bigDecimal.setScale(2, RoundingMode.HALF_UP).toFloat() // 使用 RoundingMode.HALF_UP
        return finDp
    }

    private fun makeAllDimension(type:DimenTypes,designWidth:Int):String{
        var dpValue = 0f
        var temp = ""
        val sb = StringBuilder()
        try {
            sb.append(XML_HEADER)
            sb.append(XML_RESOURCE_START)
            temp = String.format(XML_BASE_DPI,type.getSwWidthDp())
            sb.append(temp)
            for (i in 0..MAX_SIZE) {
                dpValue = px2dip(i.toFloat(), type.getSwWidthDp(), designWidth)
                temp = String.format(XML_DIMEN_TEMPLETE, "", i, dpValue)
                sb.append(temp)
            }
            sb.append(XML_RESOURCE_END)
        }catch (e:Exception){
            e.printStackTrace()
        }
        return sb.toString()
    }


    /**
     * 生成的目标文件夹
     * 只需传宽进来就行
     * @param type 枚举类型
     * @param buildDir 生成的目标文件夹
     * @param modelName 在哪个模块下生成，默认根目录，如 modelName = “app”会生成app/src/main/res/values-sw300dp
     */
    fun makeAll(designWidth: Int, type: DimenTypes, buildDir: String,modelName:String="") {
        try {
            //生成规则
            val folderName: String
            if (type.getSwWidthDp() > 0) {
                //适配Android 3.2+
                folderName = ("values-sw" + type.getSwWidthDp()).toString() + "dp"
            } else {
                return
            }

            //生成目标目录
            val file = if (modelName.isBlank()) {
                File(buildDir + File.separator + folderName)
            }else{
                File(buildDir + File.separator + "${modelName}/src/main/res/" + folderName)
            }

            if (!file.exists()) {
                file.mkdirs()
            }

            //生成values文件
            val fos = FileOutputStream(file.absolutePath + File.separator + XML_NAME)
            fos.write(makeAllDimension(type, designWidth).toByteArray())
            fos.flush()
            fos.close()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

}